#include "sendvideo.h"


sendVideo::sendVideo(QList<QString> &fileList, QString &appDirPath,QList<QPair<QString, QVector<QPair<QString, QJsonArray> > > > &listpair, QObject *parent)
    :QObject(parent)
    , m_fileList(fileList)
    , m_appDirPath(appDirPath)
    , m_listpair(listpair)
{

}

sendVideo::~sendVideo()
{

}



void sendVideo::startRun()
{
    // while(1)
    // {
    //     if(flag)
    //     {
    //         flag = false;
    // Run();
    //     }
    // }
    connect(this,&sendVideo::_start,this,&sendVideo::Run);
}


void sendVideo::Run()
{
    qDebug()<<"run";
    // 这里是线程要执行的任务代码
    for (const QString &i : m_fileList) {

        cv::VideoCapture cap(i.toStdString());
        if (!cap.isOpened()) {
            qDebug() << "无法打开视频文件: " << QString::fromStdString(i.toStdString());
        }
        int getTotalFrame = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_COUNT));
        cap.release();
        if (getTotalFrame > 2000) {
            QFileInfo fileInfo(i);
            QString fileName = fileInfo.completeBaseName();
            QString str = m_appDirPath + "/temp/" + fileName + "/";
            QDir().mkpath(str);
            QPair<QString, QVector<QPair<QString, QJsonArray>>> pair;
            QVector<QPair<QString, QJsonArray>> vector;
            if (splitVideoByFixedFrames(i.toStdString(), str.toStdString(), 2000)) {
                int num = 0;
                do {
                    QPair<QString, QJsonArray> _pair;
                    _pair.first = str + QString::number(num) + ".mp4";
                    _pair.second = QJsonArray();
                    vector.append(_pair);
                    num++;
                    getTotalFrame -= 2000;
                } while (getTotalFrame > 0);
                pair.first = i;
                pair.second = vector;
                m_listpair.append(pair);
            }

        } else {
            QPair<QString, QVector<QPair<QString, QJsonArray>>> pair;
            QVector<QPair<QString, QJsonArray>> vector;
            QPair<QString, QJsonArray> _pair;
            _pair.first = i;
            _pair.second = QJsonArray();
            vector.append(_pair);
            pair.first = i;
            pair.second = vector;
            m_listpair.append(pair);
            emit OK(i);
        }
    }
    for (const auto& outer : m_listpair) {
        qDebug() << "Outer Key:" << outer.first;
        for (const auto& inner : outer.second) {
            qDebug() << "  Inner Key:" << inner.first;
            qDebug() << "  JSON Array:" << inner.second;
        }
    }
    emit end();
}
bool sendVideo::splitVideoByFixedFrames(const std::string &inputVideoPath, const std::string &outputVideoPrefix, int framesPerSegment)
{
    cv::VideoCapture cap(inputVideoPath);
    if (!cap.isOpened()) {
        qDebug() << "无法打开输入视频文件: " << QString::fromStdString(inputVideoPath);
        return false;
    }

    int frameWidth = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
    int frameHeight = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));
    double fps = cap.get(cv::CAP_PROP_FPS);

    int frameCount = 0;
    int segmentIndex = 0;
    cv::VideoWriter writer;

    cv::Mat frame;
    while (cap.read(frame)) {
        if (frameCount % framesPerSegment == 0) {
            if (writer.isOpened()) {
                writer.release();
                QString outputVideo = QString::fromStdString(outputVideoPrefix)  + QString::number(segmentIndex-1) + ".mp4";
                emit OK(outputVideo);
            }
            std::string outputVideoPath = outputVideoPrefix  + std::to_string(segmentIndex) + ".mp4";
            writer.open(outputVideoPath, cv::VideoWriter::fourcc('X', '2', '6', '4'), fps, cv::Size(frameWidth, frameHeight));
            if (!writer.isOpened()) {
                qDebug() << "无法创建输出视频文件: " << QString::fromStdString(outputVideoPath);
                cap.release();
                return false;
            }
            segmentIndex++;
        }
        writer.write(frame);
        frameCount++;

    }

    if (writer.isOpened()) {
        writer.release();
        QString outputVideo = QString::fromStdString(outputVideoPrefix)  + QString::number(segmentIndex-1) + ".mp4";
        emit OK(outputVideo);
    }
    cap.release();
    return true;
}
